#include "stdafx.h"
#include "PacketHandler.h"
#include <Protocol_ServerCommon.h>
#include <PacketStruct.h>
#include <PacketStruct_AZ.h>
#include <PacketStruct_GZ.h>
#include <PacketStruct_DZ.h>
#include <PacketStruct_MZ.h>
#include <PacketStruct_WZ.h>

#include "Handler_FromGameServer.h"
#include "Handler_FromAgentServer.h"
#include "Handler_FromDBProxyServer.h"
#include "Handler_FromMasterServer.h"
#include "Handler_FromWorldServer.h"

PacketHandler::PacketHandler()
{
	Resister();
}

PacketHandler::~PacketHandler()
{

}

BOOL PacketHandler::Resister()
{
#define HANDLER_AGENT( c, p )	if( !AddHandler( PACKET_HANDLER_AGENT, c, p, Handler_FromAgentServer::On##p ) ) return FALSE
#define HANDLER_GAME( c, p )	if( !AddHandler( PACKET_HANDLER_GAME, c, p, Handler_FromGameServer::On##p ) ) return FALSE
#define HANDLER_DBPROXY( c, p )	if( !AddHandler( PACKET_HANDLER_DBPROXY, c, p, Handler_FromDBProxyServer::On##p ) ) return FALSE
#define HANDLER_MASTER( c, p )	if( !AddHandler( PACKET_HANDLER_MASTER, c, p, Handler_FromMasterServer::On##p ) ) return FALSE
#define HANDLER_WORLD( c, p )	if( !AddHandler( PACKET_HANDLER_WORLD, c, p, Handler_FromWorldServer::On##p ) ) return FALSE

	HANDLER_AGENT( 0, SERVERCOMMON_HEARTBEAT );
	HANDLER_AGENT( AZ_GUILD, AZ_GUILD_LOGIN_CHAR_CMD );
	HANDLER_AGENT( AZ_GUILD, AZ_GUILD_LOGOUT_CHAR_CMD );


	HANDLER_GAME( 0, SERVERCOMMON_HEARTBEAT );	
	HANDLER_GAME( GZ_GUILD, GZ_GUILD_SELECT_SYN );
	HANDLER_GAME( GZ_GUILD, GZ_GUILD_CREATE_SYN );
	HANDLER_GAME( GZ_GUILD, GZ_GUILD_DESTROY_SYN );
	HANDLER_GAME( GZ_GUILD, GZ_GUILD_JOIN_SYN );
	HANDLER_GAME( GZ_GUILD, GZ_GUILD_WITHDRAW_SYN );


	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_SELECT_ACK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_CREATE_ACK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_DESTROY_ACK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_JOIN_ACK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_WITHDRAW_ACK );


	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_SELECT_NAK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_CREATE_NAK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_DESTROY_NAK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_JOIN_NAK );
	HANDLER_DBPROXY( DZ_GUILD, DZ_GUILD_WITHDRAW_NAK );

	HANDLER_MASTER( MZ_CONNECTION, MZ_SERVER_INFO_CMD );
	HANDLER_MASTER( 0, SERVERCOMMON_SERVERSHUTDOWN_REQ );

	HANDLER_WORLD( WZ_GUILD, WZ_GUILD_SELECT_SYN );

	return TRUE;
}

BOOL PacketHandler::AddHandler( ePACKET_HANDLER PHandler, BYTE category, BYTE protocol, fnHandler fnHandler )
{
	HANDLER_FUNC * pFuncInfo	= new HANDLER_FUNC;
	pFuncInfo->m_dwFunctionKey	= MAKEWORD( category, protocol );
	pFuncInfo->m_fnHandler		= fnHandler;
	return m_FuncMap[PHandler].Add( pFuncInfo );
}

VOID PacketHandler::ParsePacket( ePACKET_HANDLER PHandler, ServerSession * pSession, MSG_BASE *pMsg, WORD wSize )
{
	if( pMsg->m_byCategory == 0xff )
	{
	}

	HANDLER_FUNC * pFuncInfo = (HANDLER_FUNC *)m_FuncMap[PHandler].Find( MAKEWORD( pMsg->m_byCategory, pMsg->m_byProtocol ) );

	if( pFuncInfo == NULL )
	{
		_PRINT( "[PacketHandler::ParsePacket] Invalid packet! Category[%u] Protocol[%u]\n", pMsg->m_byCategory, pMsg->m_byProtocol );
		ASSERT( ! "Invalid packet from Agent Server!" ) ;

		return ;
	}

	pFuncInfo->m_fnHandler( pSession, pMsg, wSize );
}
